package com.gpnu7_412.yanzhi.service.impl;

import com.gpnu7_412.yanzhi.entity.Notice;
import com.gpnu7_412.yanzhi.exception.custom.PermissionDeniedException;
import com.gpnu7_412.yanzhi.exception.custom.RequestDataException;
import com.gpnu7_412.yanzhi.exception.custom.SQLExecuteException;
import com.gpnu7_412.yanzhi.mapper.NoticeMapper;
import com.gpnu7_412.yanzhi.service.NoticeService;
import com.gpnu7_412.yanzhi.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    NoticeMapper noticeMapper;
    //查看系统通知
    @Override
    public List<Notice> getNoticeListByUserId(Integer userId) {
        if (userId == null) {
            throw new RequestDataException("用户Id获取异常");
        }
        List<Notice> noticesList = noticeMapper.selectNoticeByUserId(userId);
        if (noticesList == null) {
            throw new SQLExecuteException("数据获取异常");
        }
        return noticesList;
    }

    //根据通知ID删除通知
    @Override
    public Boolean deleteNoticeById(Integer noticeId) {
        if (noticeId == null) {
            throw new RequestDataException("通知信息获取异常");
        }
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        Notice notice = noticeMapper.selectNoticeById(noticeId);
        if (!notice.getUserId().equals(userId)) {
            throw new PermissionDeniedException("权限不足");
        }
        Integer num = noticeMapper.deleteNoticeByNoticeId(noticeId);
        if (num == 0) {
            throw new SQLExecuteException("删除操作执行异常");
        }
        return true;
    }


}
